package com.webapi.core.model.persist;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.webapi.core.model.common.PayStatus;
import com.webapi.core.model.common.PayType;
import com.webapi.core.model.dto.PayDto;

@Entity
@Table(name = "tb_pay")
public class Pay extends Base implements Serializable {

    private static final long serialVersionUID = -752234499200902141L;

    @Enumerated(EnumType.STRING)
    @Column(name = "type", length = 15, nullable = false)
    private PayType type;

    @Column(name = "amount", nullable = false, columnDefinition = "double default 0")
    private Double amount;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 15, nullable = false)
    private PayStatus status;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "pay_time")
    private Date payTime;
    
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "order_id", nullable = false, unique = true)
    private Order order;

    public PayType getType() {
        return type;
    }

    public void setType(PayType type) {
        this.type = type;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public PayStatus getStatus() {
        return status;
    }

    public void setStatus(PayStatus status) {
        this.status = status;
    }

    public Date getPayTime() {
        return payTime;
    }

    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
    
    public static Pay valueOf(Order order, PayDto payDto) {
        Pay pay = new Pay();
        pay.setAmount(payDto.getAmount());
        pay.setType(payDto.getType());
        pay.setStatus(PayStatus.Unpay);
        pay.setOrder(order);
        return pay;
    }

    @Override
    public String toString() {
        return "Pay [type=" + type + ", amount=" + amount + ", status="
                + status + ", payTime=" + payTime + ", getId()=" + getId()
                + ", getCreateTime()=" + getCreateTime() + "]";
    }

}
